#ifndef GLOBALARRAY_CU
#define GLOBALARRAY_CU
#include "global_object.h"
#include <cuda_runtime.h>


template <class T>
class GlobalArray
{
public:

	T* data_h;
	T* data_d;

	size_t size;


	inline __host__ GlobalArray(void)
	{
		size = 1;
		setData(NULL);
	}

	inline __host__ ~GlobalArray(void)
	{
	}

	inline __host__ GlobalArray(int sz)
	{
		size = sz;
	}

	inline __host__ void getFromDevice(T* buf, size_t size = sizeof(T)){
		cudaMemcpy(buf, data_d, size, cudaMemcpyDeviceToHost); 
	}

	inline __host__ __device__  T* operator=(T* rhs){
		setData(rhs);
		return getData();
	}

	inline __host__ __device__  T* operator->() {
		return getData();
	}

	inline __host__ __device__  T* operator*() {
		return *getData();
	}

	inline __host__ __device__  T& operator[](const int i) {
		return getData()[i];	
	}

	inline __host__ __device__  bool isNull(){
		return getData() == NULL;
	}

	inline __host__ void setData(T* data){
		#ifdef __CUDA_ARCH__
			data_d = data;
		#else
			data_h = data;
			updateDevice();
		#endif
	}

	inline __host__ __device__  T* getData() {
		#ifdef __CUDA_ARCH__
			return data_d;
		#else
			return data_h;
		#endif
	}
};

#endif